package nl.hu.verbrandendephoenix.client;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;

public class MenuManager {

	private RootPanel menu;
	
	public MenuManager() {
		menu = RootPanel.get("menu");
		generateMenu();
	}
	
	public void generateMenu() {
		menu.clear();
		
		String cookie = Cookies.getCookie("loginString");
		if(cookie != null) {
			final NotificationManager nM = NotificationManager.get();
			
			AppManager.get().getRpcService().validLogin(cookie, new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable caught) {
					nM.commError();
				}
				@Override
				public void onSuccess(Boolean result) {
					if(result) {
						UserMenu();
					} else {
						VisitorMenu();
					}
				}
			});
		} else {
			VisitorMenu();
		}
	}
	
	private void VisitorMenu() {
		menu.add(new HTML("<ul>" +
				addItem("Veilingen", "#searchauction") +
				"</ul>"));
		menu.add(new HTML(""));
	}
	
	private void UserMenu() {
		menu.clear();
		menu.add(new HTML("<ul>" + 
			addItem("Veilingen", "#searchauction") +
			addItem("Mijn Veilingen", "#myAuctions") +
			addItem("Veiling Starten", "#new_auction") +
		"</ul>"));
	}
	
	private String addItem(String name, String url) {
		return "<li><a href=\""+url+"\">"+name+"</a></li>";
	}
}	
	//De verschillende menu's voor de ingelogde gebruiker/moderator en niet ingelogde gebruikers
	/*public void showMenuVakantiegangerNietIngelogd(){
		Command register = new Command() {
			public void execute() {
				showRegisterForm();
			}
		};
		Command home = new Command() {
			public void execute() {
				showHome();
			}
		};
		Command login = new Command() {
			public void execute() {
				showLoginForm();
			}
		};

		Menu = new MenuBar(true);
		Menu.addItem("Home", home);
		Menu.addItem("Registreren", register);
		Menu.addItem("Inloggen", login);
		Menu.setPixelSize(100, 400);

		//Parsen
		showInMenu(Menu);
	}*/
	
	/*public void showMenuUserLoggedIn() {
		Command Home= new Command() {
			public void execute() {
				showHomeForm();
			}
		};
		Command home= new Command() {
			public void execute() {
				clearMenuDiv();
				showMenuUserLoggedIn();
				showHome();
			}
		};
		Command GegevensBeheren= new Command() {
			public void execute() {
				showGegevensBeherenForm();
			}
		};
		Command Kopen= new Command() {
			public void execute() {
				showKopenForm();
			}
		};
		Command Verkopen= new Command() {
			public void execute() {
				showVerkopenForm();
			}
		};
		Command Contact= new Command() {
			public void execute() {
				showContactForm();
			}
		};
		Command logout= new Command() {
			public void execute() {
				showMenuNotLoggedIn();
				showLoginForm();
				usernameLogIn = "";
				passwordLogin = "";
				Window.alert("U bent nu uitgelogd");
			}
		};
		
				Menu = new MenuBar(true);
		Menu.addItem("Home", home);
		Menu.addItem("Gegevens Beheren", GegevensBeheren);
		Menu.addItem("Kopen", Kopen);
		Menu.addItem("Verkopen", Verkopen);
		Menu.addItem("Neem contact met ons op", Contact);
		Menu.addItem("Uitloggen", logout);
		Menu.setPixelSize(100, 400);

		//Parsen
		showInMenu(Menu);
	}*/
	/*public void showMenuModeratorLoggedIn() {
		Command home= new Command() {
			public void execute() {
				showHome();
			}
		};

		Command User Gegevens Beheren= new Command() {
			public void execute() {
				showUserGegevensBeherenForm();
			}
		};
		Command Veilingen Beheren= new Command() {
			public void execute() {
				showVeilingenBeherenForm();
			}
		};
		Command Eigen Gegevens Beheren= new Command() {
			public void execute() {
				showEigenGegevensForm();
			}
		};
		Command logout= new Command() {
			public void execute() {
				showMenuNotLoggedIn();
				showLoginForm();
				usernameLogIn = "";
				passwordLogin = "";
				Window.alert("U bent nu uitgelogd");
			}
		};

		Menu = new MenuBar(true);
		Menu.addItem("Home", home);
		Menu.addItem("User Gegevens Beheren", UserGegevensBeheren);
		Menu.addItem("Veilingen Beheren", VeilingenBeheren);
		Menu.addItem("Eigen Gegevens Beheren", EigenGegevens);
		Menu.addItem("Uitloggen", logout);
		Menu.setPixelSize(100, 400);

		showInMenu(Menu);
	}

}*/
